<template>
  <el-dialog title="可分配角色列表" :visible.sync="dialogFormVisible" append-to-body width="500px" @close="close" :close-on-click-modal="false">
    <el-alert title="双击选中添加" type="info"></el-alert>
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.roleCode" placeholder="编码" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item>
            <el-input v-model.trim="queryForm.roleName" placeholder="名称" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      border
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @row-dblclick="save"
      @selection-change="handleSelectionChange"
    >
      <el-table-column show-overflow-tooltip align="center" min-width="50" type="selection"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="roleCode" label="编码"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="roleName" label="名称"></el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getUserRolePage, doAddUserRole, doAddUserRoles } from '@/api/system/role'

  export default {
    name: 'UserRoleAdd',
    data() {
      return {
        title: '',
        listLoading: true,
        dialogFormVisible: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        list: [],
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          roleName: undefined,
          userId: undefined,
        },
        selection: [],
      }
    },
    created() {},
    methods: {
      handleSelectionChange(selection) {
        this.selection = selection
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getUserRolePage(this.queryForm)
        this.list = data.list
        this.total = data.total
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      showEdit(userId) {
        this.queryForm.userId = userId
        this.fetchData()
        this.dialogFormVisible = true
      },
      close() {
        this.queryForm = this.$options.data().queryForm
        this.dialogFormVisible = false
      },
      save(row) {
        if (row.roleId) {
          this.$baseConfirm('你确定为该用户添加角色吗', null, async () => {
            row.userId = this.queryForm.userId
            await doAddUserRole(row).then((res) => {
              if (res.success) {
                this.$baseMessage(res.msg, 'success')
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          })
        } else if (this.selection.length != 0) {
          this.$baseConfirm('你确定为该用户添加多个角色吗', null, async () => {
            let params = {
              master: this.queryForm.userId,
              slaves: this.selection,
            }
            await doAddUserRoles(params).then((res) => {
              if (res.success) {
                this.$baseMessage(res.msg, 'success')
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
    },
  }
</script>
